package com.cskaoyan.service.impl;

import com.cskaoyan.bean.yangzihui.*;
import com.cskaoyan.mapper.WxTopicMapper;
import com.cskaoyan.service.WxTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yangzihui
 * @Description
 * @Create 2022-06-30 9:31
 * @Version
 */
@Service
@Transactional
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    WxTopicMapper wxTopicMapper;

    @Override
    public TopicPo list(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<TopicList> topicLists = wxTopicMapper.list();
        PageInfo pageInfo = new PageInfo(topicLists);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return TopicPo.data(total, pages, limit, page, topicLists);
    }

    @Override
    public TopicDetailPo detail(Integer id) {
        DetailOfTopicPo detailOfTopicPo = wxTopicMapper.detail(id);
        Integer[] goods = detailOfTopicPo.getGoods();
        List<GoodsPo> goodsPos = new ArrayList<>();
        for (Integer good : goods) {
            goodsPos = wxTopicMapper.detailGoods(good);
        }
        TopicDetailPo topicDetailPo = new TopicDetailPo();
        topicDetailPo.setTopic(detailOfTopicPo);
        topicDetailPo.setGoods(goodsPos);
        return topicDetailPo;
    }

    @Override
    public TopicDetailRelatedPo related(Integer id) {
        Integer page = 1;
        Integer limit = 4;
        PageHelper.startPage(page, limit);
        List<DetailOfTopicPo> detailOfTopicPos = wxTopicMapper.related(id);
        PageInfo pageInfo = new PageInfo(detailOfTopicPos);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return TopicDetailRelatedPo.data(total, pages, limit, page, detailOfTopicPos);
    }
}
